home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d14
/
atre12.arc
/
LF.ARC
/
LFEDITIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-27
|
9KB
|
331 lines
/***** editor I/O *****/
#include <windows.h>
#include <string.h>
#include "filedlg.h"
/* in filedlg.c */
int DoFileOpenDlg (HANDLE, WORD, char *, char *, WORD , char *, POFSTRUCT);
int DoFileSaveDlg (HANDLE, WORD, char *, char *, WORD *, char *, POFSTRUCT);
int DoOutFileDlg (HANDLE, WORD, char *, char *, WORD *, char *, POFSTRUCT);
LPSTR lstrrchr (LPSTR, char);
extern char szAppName[]; /*in lfedit.c*/
extern char szFileSpec[];
extern char szFileNameOut[];
extern char szFileNameIn[];
BOOL bUserAbort;
HWND hDlgPrint;
#pragma argsused
BOOL FAR PASCAL PrintDlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam)
{
switch (message)
{
case WM_INITDIALOG:
EnableMenuItem (GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
return TRUE;
case WM_COMMAND:
bUserAbort = TRUE;
EnableWindow(GetParent(hDlg), TRUE);
DestroyWindow(hDlg);
hDlgPrint = 0;
return TRUE;
}
return FALSE;
}
#pragma argsused
BOOL FAR PASCAL AbortProc (HDC hPrinterDC, short nCode)
{
MSG msg;
while(!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return !bUserAbort;
}
HDC GetPrinterDC (void)
{
static char szPrinter[80];
char *szDevice, *szDriver, *szOutput;
GetProfileString("windows", "device", ",,,", szPrinter, 80);
#pragma warn -pia
if (((szDevice = strtok (szPrinter, ",")) &&
(szDriver = strtok (NULL , ", "))) &&
(szOutput = strtok (NULL , ", ")))
#pragma warn .pia
return CreateDC(szDriver, szDevice, szOutput, NULL);
return 0;
}
BOOL PrintFile (HANDLE hInst, HWND hwnd, HWND hwndEdit, char *szFileName)
{
BOOL bError = FALSE;
char szMsg[40];
FARPROC lpfnAbortProc, lpfnPrintDlgProc;
HDC hdcPrn;
NPSTR psBuffer;
short yChar, nCharsPerLine, nLinesPerPage,
nTotalLines, nTotalPages, nPage, nLine, nLineNum = 0;
int nEscapeReturn;
TEXTMETRIC tm;
if (0 == (nTotalLines = (short) SendMessage(hwndEdit,
EM_GETLINECOUNT, 0 , 0L)))
return FALSE;
if (NULL == (hdcPrn = GetPrinterDC()))
return TRUE;
GetTextMetrics (hdcPrn, &tm) ;
yChar = tm.tmHeight + tm.tmExternalLeading;
nCharsPerLine = GetDeviceCaps (hdcPrn, HORZRES) / tm.tmAveCharWidth;
nLinesPerPage = GetDeviceCaps (hdcPrn, VERTRES) / yChar;
nTotalPages = (nTotalLines + nLinesPerPage - 1) / nLinesPerPage;
psBuffer = (NPSTR) LocalAlloc(LPTR, nCharsPerLine);
EnableWindow(hwnd, FALSE);
bUserAbort = FALSE;
lpfnPrintDlgProc = MakeProcInstance(PrintDlgProc, hInst);
hDlgPrint = CreateDialog(hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc);
SetDlgItemText (hDlgPrint, IDD_FNAME, szFileName);
lpfnAbortProc = MakeProcInstance (AbortProc, hInst);
Escape (hdcPrn, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL);
strcat (strcat (strcpy (szMsg, szAppName), " - "), szFileName);
nEscapeReturn = Escape(hdcPrn, STARTDOC, strlen(szMsg), szMsg, NULL);
if (nEscapeReturn < 0)
bError = TRUE;
else
{
for (nPage = 0; nPage < nTotalPages; nPage++)
{
for (nLine = 0; nLine < nLinesPerPage &&
nLineNum < nTotalLines; nLine++, nLineNum++)
{
*(short *) psBuffer = nCharsPerLine;
TextOut(hdcPrn, 0, yChar * nLine, psBuffer,
(short) SendMessage (hwndEdit, EM_GETLINE,
nLineNum, (LONG) (LPSTR) psBuffer));
}
nEscapeReturn = Escape(hdcPrn, NEWFRAME, 0, NULL, NULL) ;
if (nEscapeReturn < 0)
{
bError = TRUE;
break;
}
#pragma warn -rch
if (bUserAbort)
break;
#pragma warn .rch
}
}
if (!bError)
Escape(hdcPrn, ENDDOC, 0, NULL, NULL);
if (!bUserAbort)
{
EnableWindow(hwnd, TRUE);
DestroyWindow(hDlgPrint);
}
if (bError || bUserAbort)
{
strcat(strcpy(szMsg, "Could not print: "), szFileName);
MessageBox (hwnd, szMsg, szAppName, MB_OK | MB_ICONEXCLAMATION);
}
LocalFree ((LOCALHANDLE) psBuffer);
FreeProcInstance (lpfnPrintDlgProc);
FreeProcInstance (lpfnAbortProc);;
DeleteDC (hdcPrn);
return bError || bUserAbort;
}
long FileLength (HANDLE hFile)
{
long lCurrentPos = _llseek(hFile, 0L, 1);
long lFileLength = _llseek(hFile, 0L, 2);
_llseek(hFile, lCurrentPos, 0);
return lFileLength;
}
void OkMessageBox(HWND hwnd, char *szString, char *szFileName)
{
char szBuffer [40];
wsprintf (szBuffer, szString, (LPSTR) szFileName);
MessageBox(hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION);
}
BOOL GetOutFile(HANDLE hInst, HWND hwnd)
{
char szBuffer[40];
char szFileName[16];
WORD wStatus;
char szOutFileSpec[] = "*.OUT";
char szOutExt [] = ".OUT";
OFSTRUCT of;
/*
lstrcpy(szOutFileSpec, szFileNameIn);
*(lstrrchr(szOutFileSpec, '.')) = '\0';
lstrcat(szOutFileSpec, szOutExt);
*/
if (!DoOutFileDlg (hInst, hwnd, szOutFileSpec, szOutExt,
&wStatus, szFileName, &of))
return FALSE;
if (wStatus == 1)
{
wsprintf(szBuffer, "Replace existing %s", (LPSTR) szFileName);
if (IDNO == MessageBox(hwnd, szBuffer, szAppName,
MB_YESNO | MB_ICONQUESTION))
return FALSE;
}
lstrcpy (szFileNameOut, of.szPathName);
return TRUE;
}
BOOL ReadFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
char *szFileName, BOOL bAskName)
{
DWORD dwLength;
HANDLE hFile, hTextBuffer;
LPSTR lpTextBuffer;
if (bAskName)
{
if (!DoFileOpenDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
0x4010, szFileName, pof))
return FALSE;
}
#pragma warn -rng
if (-1 == (hFile = OpenFile(szFileName, pof, OF_READ | OF_REOPEN)))
{
OkMessageBox(hwnd, "Cannot open file %s", szFileName);
return FALSE;
}
if ((dwLength = FileLength(hFile)) >= 64000)
{
_lclose(hFile);
OkMessageBox(hwnd, "File %s too large", szFileName);
return FALSE;
}
if (NULL == (hTextBuffer = GlobalAlloc(GHND, (DWORD) dwLength + 1)))
{
_lclose(hFile);
OkMessageBox(hwnd,"Not enough memory for %s", szFileName);
return FALSE;
}
lpTextBuffer = GlobalLock (hTextBuffer);
_lread(hFile, lpTextBuffer, (WORD) dwLength);
_lclose(hFile);
lpTextBuffer [(WORD) dwLength] = '\0';
SetWindowText (hwndEdit, lpTextBuffer);
GlobalUnlock (hTextBuffer);
GlobalFree(hTextBuffer);
return TRUE;
}
BOOL WriteFile (HANDLE hInstance, HWND hwnd, HWND hwndEdit, POFSTRUCT pof,
char *szFileName, BOOL bAskName)
{
char szBuffer[40];
HANDLE hFile, hTextBuffer;
NPSTR npTextBuffer;
WORD wStatus, wLength;
if (bAskName)
{
if(!DoFileSaveDlg (hInstance, hwnd, szFileSpec, szFileSpec + 1,
&wStatus, szFileName, pof))
return(FALSE);
if (wStatus == 1)
{
wsprintf (szBuffer, "Replace existing %s", (LPSTR) szFileName);
if (IDNO == MessageBox(hwnd, szBuffer, szAppName,
MB_YESNO | MB_ICONQUESTION))
return FALSE;
}
}
else
OpenFile (szFileName, pof, OF_PARSE);
if (-1 == (hFile = OpenFile(szFileName, pof, OF_CREATE | OF_REOPEN)))
{
OkMessageBox(hwnd, "Cannot create file %s", szFileName);
return FALSE;
}
#pragma warn .rng
wLength = GetWindowTextLength(hwndEdit);
hTextBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
npTextBuffer = LocalLock(hTextBuffer);
if (wLength != _lwrite(hFile, npTextBuffer, wLength))
{
_lclose(hFile);
OkMessageBox(hwnd, "Cannot write file %s to disk: ", szFileName);
return FALSE;
}
_lclose(hFile);
LocalUnlock(hTextBuffer);
return TRUE;
}